<template>
  <div>
    <div class="top-box">
      <el-button type="success" @click="add">添加链接</el-button>
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.id" placeholder="请输入编号" class="user-input"></el-input>
			<el-input v-model="searchParam.name" placeholder="请输入链接名称" class="user-input"></el-input>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>

      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
		<el-table-column label="链接名称" prop="name"></el-table-column>
		<el-table-column label="链接英文名称" prop="en_name"></el-table-column>
		<el-table-column label="链接地址" prop="link"></el-table-column>
		<el-table-column label="图标" prop="icon"></el-table-column>
		<el-table-column label="位置" prop="position_desc"></el-table-column>
		<el-table-column label="排序" prop="sort"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <i class="i-icon icon-revamp" title="修改"  @click="edit(scope.row)"></i>
            <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
    </div>
    <el-dialog title="添加链接" :visible.sync="showAddModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>链接名称：</label>
        <div class="input-group-box"><el-input v-model="addParam.name" placeholder="请输入链接名称"></el-input></div>
      </div>
      <div class="input-group">
        <label><i>*</i>链接英文名称：</label>
        <div class="input-group-box"><el-input v-model="addParam.en_name" placeholder="请输入链接英文名称"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>链接地址：</label>
        <div class="input-group-box"><el-input v-model="addParam.link" placeholder="请输入链接地址"></el-input></div>
      </div>
	  <div class="input-group">
        <label><i>*</i>图标：</label>
        <div class="input-group-box">
          <el-radio-group v-model="addParam.icon">
            <el-radio :label="item" :key="index" v-for="(item, index) in icons">
              <i :class="item"></i>
            </el-radio>
          </el-radio-group>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>位置：</label>
        <div class="input-group-box">
          <el-select v-model="addParam.position" placeholder="请选择位置" class="user-input">
            <el-option
              v-for="item in positionOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
        </div>
      </div>

      <div class="input-group">
        <label>排序：</label>
        <div class="input-group-box"><el-input v-model="addParam.sort" placeholder="输入排序值" type="number"></el-input><span style="color: red;">排序值越大越靠前</span></div>
      </div>


      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改链接" :visible.sync="showUpdateModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>链接名称：</label>
        <div class="input-group-box"><el-input v-model="editParam.name" placeholder="请输入链接名称"></el-input></div>
      </div>
      <div class="input-group">
        <label><i>*</i>链接英文名称：</label>
        <div class="input-group-box"><el-input v-model="editParam.en_name" placeholder="请输入链接英文名称"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>链接地址：</label>
        <div class="input-group-box"><el-input v-model="editParam.link" placeholder="请输入链接地址"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>图标：</label>
        <div class="input-group-box">
          <el-radio-group v-model="editParam.icon">
            <el-radio :label="item" :key="index" v-for="(item, index) in icons">
              <i :class="item"></i>
            </el-radio>
          </el-radio-group>
        </div>
      </div>
      <div class="input-group">
        <label><i>*</i>位置：</label>
        <div class="input-group-box">
          <el-select v-model="editParam.position" placeholder="请选择位置" class="user-input">
            <el-option
              v-for="item in positionOptions"
              :key="item.key"
              :label="item.value"
              :value="item.key">
            </el-option>
          </el-select>
        </div>
      </div>

      <div class="input-group">
        <label>排序：</label>
        <div class="input-group-box"><el-input v-model="editParam.sort" placeholder="输入排序值" type="number"></el-input><span style="color: red;">排序值越大越靠前</span></div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="showUpdateModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import apiConfig from '../../utils/apiconfig'
import api from '../../utils/api'
import helper from '../../utils/helper'
import loading from '../../components/loading'
export default {
  data () {
    return {
      listData: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      showAddModelFlag: false,
      showUpdateModelFlag: false,
      searchParam: {
        id: '',
        name: ''
      },
      addParam: {
        name: '',
        en_name: '',
        link: '',
        icon: '',
        position: '',
        sort: 0
      },
      editParam: {
        id: '',
        name: '',
        en_name: '',
        link: '',
        icon: '',
        position: '',
        sort: 0
      },
      icons: ['icon-rule', 'icon-vip', 'icon-bill', 'icon-process', 'icon-table', 'icon-question'],
      positionOptions: []
    }
  },
  created () {
    this.getLinkPostionOption()
    this.getList()
  },
  components: {
    loading
  },
  methods: {
    getSearchParam () { // 参数
      var param = {
        page: this.page,
        id: this.searchParam.id,
        name: this.searchParam.name
      }
      this.params = param
    },
    getList () {
      this.getSearchParam()
      api.GET(helper.getManageApiUrl(apiConfig.linkList), this.params, res => {
        this.listData = res
        this.loading = false
      }, res => {
        console.log(res)
      })
    },
    search () {
      this.getList()
    },
    cancel () {
      this.searchParam.name = ''
      this.searchParam.link = ''
      this.searchParam.icon = ''
      this.searchParam.position = ''
      this.searchParam.sort = ''
    },
    handleChange (val) {
      this.page = val
      this.getList()
    },
    add () {
      this.showAddModelFlag = true
    },
    confirmAdd () {
      var param = {
        name: this.addParam.name,
        en_name: this.addParam.en_name,
        link: this.addParam.link,
        icon: this.addParam.icon,
        position: this.addParam.position,
        sort: this.addParam.sort
      }
      api.GET(helper.getManageApiUrl(apiConfig.addLink), param, res => {
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 1500
        })
        this.addParam.name = ''
        this.addParam.en_name = ''
        this.addParam.link = ''
        this.addParam.icon = ''
        this.addParam.position = ''
        this.addParam.sort = 0
        this.showAddModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'fail',
          duration: 1500
        })
      })
    },
    remove (id) {
      var param = {
        id: id
      }
      this.$confirm('确认要删除此条信息吗？')
      .then(_ => {
        api.GET(helper.getManageApiUrl(apiConfig.deleteLink), param, res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 1500
          })
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: '删除失败',
            type: 'error',
            duration: 1500
          })
        })
      })
      .catch(_ => {})
    },
    edit (item) {
      this.editParam.id = item.id
      this.editParam.name = item.name
      this.editParam.en_name = item.en_name
      this.editParam.link = item.link
      this.editParam.icon = item.icon
      this.editParam.position = item.position
      this.editParam.sort = item.sort
      this.showUpdateModelFlag = true
    },
    save () {
      api.GET(helper.getManageApiUrl(apiConfig.updateLink), this.editParam, res => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 1500
        })
        this.showUpdateModelFlag = false
        this.getList()
      }, res => {
        console.log(res)
        this.$notify({
          title: '失败',
          message: res.data.msg,
          type: 'error',
          duration: 1500
        })
      })
    },
    getLinkPostionOption () {
      var param = {
        param: ''
      }
      api.GET(helper.getManageApiUrl(apiConfig.linkPositionOption), param, res => {
        this.positionOptions = res
      }, res => {
        console.log(res)
      })
    }
  }
}
</script>
<style>
 .search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }
</style>

